﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;
using com.opttek.optquest;

namespace ConsoleApplication1
{
    class WorkerThread
    {
        KCOptQuest o;
        ManualResetEvent e;
        public int pid;
        public WorkerThread(KCOptQuest myopt, ManualResetEvent ev, int pid)
        {
                o = myopt;
                e = ev;
                this.pid = pid;
                
        }

        public void work()
        {
            Thread.CurrentThread.Name = "Worker " + pid;
            COptQuestSolution solution;
            try
            {
                solution = (COptQuestSolution)o.pendingQ.Dequeue();
            }
            catch (InvalidOperationException)
            {
                return;
            }

            //    e.Reset();
            if (solution == null)
            {
                Console.WriteLine("The solution is null");
                return;
            }

            solution = o.calcEvaluate(solution, pid);
            o.completedQ.Enqueue(solution);

            e.Set();
        }



    }
}



        //    public void work()
        //    {
        //        COptQuestSolution solution;
        //        try
        //        {
        //            solution = (COptQuestSolution)o.pendingQ.Dequeue();
        //        }
        //        catch (InvalidOperationException exc)
        //        {
        //            return;
        //        }

        //    //    e.Reset();
        //        if (solution == null)
        //        {
        //            Console.WriteLine("The solution is null");
        //            return;
        //        }
                 
                
        //        double result;
        //        double x1, x2, x3, x4;
        //        Thread.Sleep(10);

        //        // Get the solution by getting the current value 
        //        // for each variable
        //        lock (this)
        //        { 
        //            x1 = solution.GetVariableValue(o.X1);
        //            x2 = solution.GetVariableValue(o.X2);
        //            x3 = solution.GetVariableValue(o.X3);
        //            x4 = solution.GetVariableValue(o.X4);
        //        }

        //        // Evaluate the objective function
        //        result = 100 * Math.Pow(x2 - Math.Pow(x1, 2), 2) + Math.Pow(1 - x1, 2) + 90 * Math.Pow(x4 -
        //                    Math.Pow(x3, 2), 2) + Math.Pow(1 - x3, 2) + 10.1 * (Math.Pow(x2 - 1, 2) +
        //                    Math.Pow(x4 - 1, 2)) + 19.8 * (x2 - 1) * (x4 - 1);

        //        // Set the objective result
        //        solution.SetObjectiveValue(result);
        //        o.completedQ.Enqueue(solution);
        //        e.Set();
        //    }
        //}